package com.orangelife.mobile.myhouse.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.curry.android.adapter.FatherAdapter;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.myhouse.activity.HouseRentActivity;
import com.orangelife.mobile.myhouse.biz.HouseBiz;
import com.orangelife.mobile.myhouse.biz.HouseHttpBiz;
import com.orangelife.mobile.myhouse.entity.IndustryEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 租客房屋认证界面的列表的适配器
 * Created by wgf on 2016/6/24.
 *
 * @author wgf
 * @since v3.8.1
 */
public class HouseRentAdapter extends FatherAdapter {

    private Context mContext;
    private List<IndustryEntity.ApartmentsBean> mList;


    public HouseRentAdapter(Context context) {
        this.mContext = context;
        this.mList = new ArrayList<>();
    }

    @Override
    public BaseAdapter setList(List list) {
        if (list != null) {
            mList.clear();
            mList.addAll(list);
            mList.add(new IndustryEntity.ApartmentsBean());
        }
        return this;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lv_house_rent, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        showLastItem(pos, holder);
        return convertView;
    }

    private void showLastItem(int pos, ViewHolder holder) {
        if (pos == mList.size() - 1) {
            holder.tvCertOthers.setVisibility(View.VISIBLE);
            holder.tvHouse.setVisibility(View.GONE);
            holder.tvIdentity.setVisibility(View.GONE);
            holder.tvCertOthers.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    jumpToTenantCert2();
                }
            });
        } else {
            holder.tvCertOthers.setVisibility(View.GONE);
            holder.tvHouse.setVisibility(View.VISIBLE);
            holder.tvIdentity.setVisibility(View.VISIBLE);
            holder.tvHouse.setText(mList.get(pos).getHouseInfo());
            holder.tvIdentity.setText(HouseBiz.getIdentityName(mContext, mList.get(pos).getType()));
        }
    }

    private void jumpToTenantCert2() {
        if(mContext instanceof HouseRentActivity){
            ((HouseRentActivity)mContext).jumpToCert2();
        }
    }


    static class ViewHolder {
        @Bind(R.id.tvHouse)
        TextView tvHouse;
        @Bind(R.id.tvIdentity)
        TextView tvIdentity;
        @Bind(R.id.tvCertOthers)
        TextView tvCertOthers;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
